/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';
import React, {
    Component,
} from 'react';

import {
  Dimensions,
  StyleSheet,
  Text,
  TouchableHighlight,
  View,
  InteractionManager
} from 'react-native';

import LoadingView from '../components/loadingView';
import {Actions,ActionConst} from 'react-native-router-flux';
import Camera from 'react-native-camera';
import CommonStyles from '../styles/common';
import Colors from '../styles/colors';
import Toast from '../utils/toast';
import Ionicons  from 'react-native-vector-icons/Ionicons';

const {width,height}=Dimensions.get('window');

export default class TakePhoto extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <View style={styles.container}>
                <Camera
                    ref='camera'
                    style={styles.preview}
                    aspect={Camera.constants.Aspect.fill}
                    captureTarget={Camera.constants.CaptureTarget.memory}
                    captureQuality={Camera.constants.CaptureQuality["480p"]}
                    type={Camera.constants.Type.back}
                    flashMode={Camera.constants.FlashMode.auto}>
                        <TouchableHighlight
                            ref='btn'
                            style={styles.capture}
                            underlayColor={Colors.default_theme}
                            onPress={this.takePhoto.bind(this)}
                            disabled={false}>
                            <Ionicons style={{color:'#fff'}} name='md-camera' size={48} />
                        </TouchableHighlight>
                </Camera>
            </View>
        );
    }

    takePhoto() {
        this.refs.btn.disabled=true;
        InteractionManager.runAfterInteractions(() => {
            this.refs.camera&&this.refs.camera.capture()
            .then((data) => {
                this.refs.btn.disabled=false;
                Actions.pop({refresh: { imageBase64: data.data }});
            })
            .catch(err => {
                this.refs.btn.disabled=false;
                Toast.showShort(err.message);
            });
        });
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1
    },
    preview: {
        flex: 1,
        justifyContent: 'flex-end',
        alignItems: 'center',
        height: Dimensions.get('window').height,
        width: Dimensions.get('window').width
    },
    capture: {
        flex: 0,
        borderRadius: 5,
        padding: 10,
        margin: 40
    }
});
